package com.stylefeng.guns.modular.channel.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.channel.service.ILoanOrderService;
import com.stylefeng.guns.modular.system.dao.LoanOrderMapper;
import com.stylefeng.guns.modular.system.model.LoanOrder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-08-28
 */
@Service
public class LoanOrderServiceImpl extends ServiceImpl<LoanOrderMapper, LoanOrder> implements ILoanOrderService {

    /**
     * 统计申请量
     * @param channelId
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public int selectCountRequest(Long channelId, Date beginDate, Date endDate) {
        EntityWrapper<LoanOrder> wrapper = new EntityWrapper<>();

//        wrapper.and("check_status>{0}", 0);
        wrapper.between(beginDate != null && endDate != null,"create_time", beginDate, endDate);
        wrapper.andNew("channel_id={0}", channelId);

        int selectCount = super.selectCount(wrapper);
        int count = this.selectCount(wrapper);
        List<LoanOrder> loanOrders = selectList(wrapper);


        return super.selectCount(wrapper);
    }

    /**
     * 统计成交量
     * @param channelId
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public int selectCountBargain(Long channelId, Date beginDate, Date endDate) {
        EntityWrapper<LoanOrder> wrapper = new EntityWrapper<>();
        wrapper.where("channel_id={0}", channelId);
        wrapper.and("order_status={0}", 3);
        wrapper.and("check_status={0}", 1);
        wrapper.between(beginDate != null && endDate != null,"create_time", beginDate, endDate);
        return super.selectCount(wrapper);
    }

    /**
     * 统计成交额
     * @param channelId
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public BigDecimal selectCountBargainAmount(Long channelId, Date beginDate, Date endDate) {
        EntityWrapper<LoanOrder> wrapper = new EntityWrapper<>();
        wrapper.where("channel_id={0}", channelId);
        wrapper.and("order_status={0}", 3);
        wrapper.and("check_status={0}", 1);
        wrapper.between(beginDate != null && endDate != null,"create_time", beginDate, endDate);
        List<LoanOrder> loanOrders = super.selectList(wrapper);
        BigDecimal bargainAmount = new BigDecimal(0.00);
        for (LoanOrder loanOrder: loanOrders) {
            bargainAmount = bargainAmount.add(loanOrder.getLoanMoney());
        }
        return bargainAmount;
    }
}
